package com.shwread.httpsdk.http.face;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shwread.httpsdk.constant.ResultCode;
import com.shwread.httpsdk.http.base.AbstractHttpPostDracom;
import com.shwread.httpsdk.http.callback.ActionListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import logic.bean.ReadBook;
import logic.constants.AppConfig;
import logic.constants.Const;

/**
 * 查询最近阅读书籍列表
 */
public class QryReadBookListAction extends AbstractHttpPostDracom {
    private Long id; //客户端查询增量数据时必填，需传递客户端本地库存的最大有效id以便服务端做比对
    private int pageNum;
    private int length;

    /**
     * 查询最近阅读书籍列表
     *
     * @param context
     * @param id    客户端查询增量数据时必填，需传递客户端本地库存的最大有效id以便服务端做比对
     * @param pageNum
     * @param length
     * @param actionListener
     */
    public QryReadBookListAction(Context context, Long id, int pageNum, int length, ActionListener actionListener) {
        super(context, "qryReadBookList.do", actionListener);
        this.id = id;
        this.pageNum = pageNum;
        this.length = length;
    }

    @Override
    public void getParameter(HashMap<String, String> parameter_map) {
        parameter_map.put("enterpriseId", AppConfig.getEnterpriseId());
        parameter_map.put("pageNum", String.valueOf(pageNum));
        parameter_map.put("length", String.valueOf(length));
        parameter_map.put("id", String.valueOf(id));
        parameter_map.put("account", Const.phone_number);
        parameter_map.put("timeStamp", String.valueOf(timeStamp));
        parameter_map.put("cid", cid(Const.phone_number + Const.ACCOUNTTYPE + Const.CLIENTTYPE + timeStamp + AppConfig.getEnterpriseId() + pageNum + length));
    }

    @Override
    public void doResponseXml(String xml) {
        println("QryReadBookListAction --> " + xml);
        int result;
        try {
            JSONObject jsonObject = new JSONObject(xml);
            result = jsonObject.getInt("resultCode");
            if (result == 1000) {
                List<ReadBook> books;
                Gson gson = new Gson();
                final java.lang.reflect.Type type = new TypeToken<List<ReadBook>>() {
                }.getType();
                books = gson.fromJson(jsonObject.getJSONObject("returnObject").getString("readBookList"), type);
                listener.OK(books);
            } else {
                listener.ERROR(result, ResultCode.getResultText(result));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doRequestHeader(HashMap<String, String> header_map) {

    }

    @Override
    public void dispose() {

    }

}
